package com.dao.impl;

import com.dao.IMessageDAO;
import com.vo.Message;
import com.db.ConnectionManager;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class MessageDAOImpl implements IMessageDAO {
    @Override
    public void addMessage(Message message) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = ConnectionManager.getConnection();
            String sql = "INSERT INTO message(title, content, writer, writeDate) VALUES(?,?,?,?)";
            pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pstmt.setString(1, message.getTitle());
            pstmt.setString(2, message.getContent());
            pstmt.setString(3, message.getWriter());
            pstmt.setTimestamp(4, new Timestamp(message.getWriteDate().getTime()));
            pstmt.executeUpdate();

            ResultSet rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                message.setMessageID(rs.getInt(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeAll(conn, pstmt, null);
        }
    }

    @Override
    public void deleteMessage(int messageID) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = ConnectionManager.getConnection();
            String sql = "DELETE FROM message WHERE messageID=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, messageID);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeAll(conn, pstmt, null);
        }
    }

    @Override
    public Message getMessageById(int messageID) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Message message = null;

        try {
            conn = ConnectionManager.getConnection();
            String sql = "SELECT * FROM message WHERE messageID=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, messageID);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                message = new Message();
                message.setMessageID(rs.getInt("messageID"));
                message.setTitle(rs.getString("title"));
                message.setContent(rs.getString("content"));
                message.setWriter(rs.getString("writer"));
                message.setWriteDate(rs.getTimestamp("writeDate"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeAll(conn, pstmt, rs);
        }
        return message;
    }

    @Override
    public List<Message> getAllMessages() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Message> messages = new ArrayList<>();

        try {
            conn = ConnectionManager.getConnection();
            stmt = conn.createStatement();
            String sql = "SELECT * FROM message ORDER BY writeDate DESC";
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                Message message = new Message();
                message.setMessageID(rs.getInt("messageID"));
                message.setTitle(rs.getString("title"));
                message.setContent(rs.getString("content"));
                message.setWriter(rs.getString("writer"));
                message.setWriteDate(rs.getTimestamp("writeDate"));
                messages.add(message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeAll(conn, stmt, rs);
        }
        return messages;
    }
}